package com.yyds.yygh.hospclient.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yyds.yygh.hospclient.repository.DepartmentRepository;
import com.yyds.yygh.hospclient.service.DepartmentService;
import com.yyds.yygh.model.hosp.Department;
import com.yyds.yygh.vo.hosp.DepartmentQueryVo;
import com.yyds.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;
    @Override
    public void save(Map<String, Object> map) {
        Department department = JSONObject.parseObject(JSONObject.toJSONString(map), Department.class);
        Department hasDepartment = departmentRepository.findByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        //判断科室是否存在
        if (hasDepartment == null){
            department.setIsDeleted(0);
            department.setUpdateTime(new Date());
            departmentRepository.save(department);
        }else {
            Date time = new Date();
            hasDepartment.setIsDeleted(0);
            hasDepartment.setCreateTime(time);
            hasDepartment.setUpdateTime(time);
            departmentRepository.save(hasDepartment);
        }
    }

    @Override
    public Page<Department> findDepartment(int pageNum, int pageSize, DepartmentQueryVo departmentQueryVo) {
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);

        Sort sort = Sort.by(Sort.Direction.DESC,"createTime");

        Pageable pageable = PageRequest.of(pageNum - 1,pageSize,sort);



        ExampleMatcher matcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        Example<Department> example = Example.of(department,matcher);

        Page<Department> page = departmentRepository.findAll(example, pageable);

        return page;
    }

    @Override
    public void removeDepartment(String hoscode, String depcode) {
        Department department = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        if (department!=null){
            departmentRepository.delete(department);
        }
    }

    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //封装最终的返回结果集
        List<DepartmentVo> list = new ArrayList<>();
        //根据医院编号查询科室
        Department department = new Department();
        Example<Department> departmentExample = Example.of(department);
        List<Department> foundDepartment = departmentRepository.findAll(departmentExample);
        //根据科室的Bigcode排序
        Map<String, List<Department>> listMap = foundDepartment.stream().collect(Collectors.groupingBy(e -> e.getBigcode()));
        for(Map.Entry<String,List<Department>> entry : listMap.entrySet()){
            //获取map键:bigcode
            String bigCode = entry.getKey();
            //获取值: bigcode对应的department集合
            List<Department> departmentList = entry.getValue();
            //获取集合中的元素
            Department dept = departmentList.get(0);
            //创建返回的DepartmentVo对象
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(bigCode);
            departmentVo.setDepname(dept.getDepname());
            //创建子科室的集合
            List<DepartmentVo> children = new ArrayList<>();
            for (Department depart : departmentList) {
                DepartmentVo childrenVo = new DepartmentVo();
                childrenVo.setDepcode(depart.getDepcode());
                childrenVo.setDepname(depart.getDepname());
                children.add(childrenVo);
            }
            //将子科室集合放入Vo对象中
            departmentVo.setChildren(children);
            //将Vo对象放入最终返回结果集中
            list.add(departmentVo);
        }
        return list;
    }

    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        return department.getDepname();
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        Department department = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        return department;
    }


}
